package com.itheima.reggie.interceptor;


import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.R;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@Component
public class LoginCheckInterceptor implements HandlerInterceptor {
    /**
     * 拦截资源之前
     * @return boolean 返回true则表示放行, 反正不放行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取登录用户
        Long empId = (Long) request.getSession().getAttribute("employee");
        if (empId == null) {
            //没有--不放行且响应给用户跳转到登录页面
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
            return false;
        }
        return true;
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}